$(function() {
	var stringTime = "2017-05-01 10:21:12";  
	var timestamp2 = Date.parse(new Date(stringTime));  
	timestamp2 = timestamp2 / 1000; 

	//2014-07-10 10:21:12的时间戳为：1404958872   
	console.log(stringTime + "的时间戳为：" + timestamp2);
	var timestamp = Date.parse(new Date());  
	var timestamp1 = timestamp / 1000;  
	var $dataJson = {
		"data": [
			{
				"imgurl": "http://img.hb.aicdn.com/5f0768ddc4e94e450f159bde8418393224ce9aa444a34-dkQh6g_fw658",
				"intro":"春眠不觉晓，处处闻啼鸟。",
				"total": "15",
				"time":timestamp2
			},
			{
				"imgurl": "img/boximg.png",
				"intro":"LPL新晋美女解说Ritaz最新宣传照颜值高身材姣好",
				"total": "15",
				"time":timestamp1
			},
			{
				"imgurl": "http://img.hb.aicdn.com/5f0768ddc4e94e450f159bde8418393224ce9aa444a34-dkQh6g_fw658",
				"intro":"春眠不觉晓，处处闻啼鸟。",
				"total": "15",
				"time":timestamp2
			},
			{
				"imgurl": "img/boximg.png",
				"intro":"LPL新晋美女解说Ritaz最新宣传照颜值高身材姣好",
				"total": "15",
				"time":timestamp1
			},
			{
				"imgurl": "https://img.alicdn.com/imgextra/i4/776109305/TB2fRofpHBmpuFjSZFAXXaQ0pXa_!!776109305.jpg",
				"intro":"LPL新晋美女解说Ritaz最新宣传照颜值高身材姣好",
				"total": "10",
				"time":timestamp1
			},
			{
				"imgurl": "img/img.png",
				"intro":"美女戴小唯泳装秀摄影阳光迷人写真",
				"total": "8",
				"time":timestamp1
			},
			{
				"imgurl": "img/beaty.png",
				"intro":"波多野结衣老师玩COS无法自拔，穿上女仆装可爱迷人",
				"total": "5",
				"time":timestamp2
			},
			{
				"imgurl": "https://img.alicdn.com/imgextra/i1/776109305/TB202yHnbFlpuFjy0FgXXbRBVXa_!!776109305.jpg",
				"intro":"前凸后翘的嫩模",
				"total": "5",
				"time":timestamp2
			},
			{
				"imgurl": "https://img.alicdn.com/imgextra/i1/776109305/TB2MyuEnmxjpuFjSszeXXaeMVXa_!!776109305.jpg",
				"intro":"福利美图送不停",
				"total": "3",
				"time":timestamp2
			},
			{
				"imgurl": "https://img.alicdn.com/imgextra/i1/776109305/TB29ea4nl8lpuFjSspaXXXJKpXa_!!776109305.jpg",
				"intro":"冰肌雪骨俏佳人。",
				"total": "3",
				"time":timestamp2
			},
			{
				"imgurl": "https://img.alicdn.com/imgextra/i3/442760519/TB2M_qispXXXXb6XpXXXXXXXXXX_!!442760519.jpg",
				"intro":"私藏美女壁纸大放送，不得了！",
				"total": "9",
				"time":timestamp2
			},
			{
				"imgurl": "img/boximg.png",
				"intro":"LPL新晋美女解说Ritaz最新宣传照颜值高身材姣好",
				"total": "15",
				"time":timestamp1
			},
			{
				"imgurl": "https://img.alicdn.com/imgextra/i4/776109305/TB2fRofpHBmpuFjSZFAXXaQ0pXa_!!776109305.jpg",
				"intro":"LPL新晋美女解说Ritaz最新宣传照颜值高身材姣好",
				"total": "10",
				"time":timestamp1
			},
			{
				"imgurl": "img/img.png",
				"intro":"美女戴小唯泳装秀摄影阳光迷人写真",
				"total": "8",
				"time":timestamp1
			},
			{
				"imgurl": "img/beaty.png",
				"intro":"波多野结衣老师玩COS无法自拔，穿上女仆装可爱迷人",
				"total": "5",
				"time":timestamp2
			},
			{
				"imgurl": "https://img.alicdn.com/imgextra/i1/776109305/TB202yHnbFlpuFjy0FgXXbRBVXa_!!776109305.jpg",
				"intro":"前凸后翘的嫩模",
				"total": "5",
				"time":timestamp2
			},
			{
				"imgurl": "https://img.alicdn.com/imgextra/i1/776109305/TB2MyuEnmxjpuFjSszeXXaeMVXa_!!776109305.jpg",
				"intro":"福利美图送不停",
				"total": "3",
				"time":timestamp2
			},
			{
				"imgurl": "https://img.alicdn.com/imgextra/i1/776109305/TB29ea4nl8lpuFjSspaXXXJKpXa_!!776109305.jpg",
				"intro":"冰肌雪骨俏佳人。",
				"total": "3",
				"time":timestamp2
			},
			{
				"imgurl": "https://img.alicdn.com/imgextra/i3/442760519/TB2M_qispXXXXb6XpXXXXXXXXXX_!!442760519.jpg",
				"intro":"私藏美女壁纸大放送，不得了！",
				"total": "9",
				"time":timestamp2
			}

		]
	}
	create($dataJson.data);
	/*----------动态生成HTML---------*/
	function create(data) {
		console.log(data.length);
		$.each(data, function(i, n){
			var boxA = $("<a class='box'></a>");
			var imgDiv = $("<div class='img-box'></div>").appendTo(boxA);
			var infoP = $("<p class='intro'></p>").appendTo(boxA);
			infoP.html(n.intro);
			var img = $("<img />").appendTo(imgDiv);
			img.attr({
				src: n.imgurl
			});
			img.on("load",function(event){
				var img_w = $(this).width();
				var img_h = $(this).height();
				img.attr({'height': img_h});
				console.log(img_h)
				boxA.css({'height': img_h + infoP.height() })
				waterFall('waterFall', 'box');
			});
			boxA.appendTo($("#waterFall"));
			var coverDiv = $("<div class='cover'></div>").appendTo(imgDiv);
			var totalP = $("<p class='top-total'></p>").appendTo(coverDiv);
			totalP.html(n.total + '张');
			var timeP = $("<p class='time'></p>").appendTo(coverDiv);
			timeP.html(n.time);
		});
			
		
	}

	/*-------划入出现信息-------*/
	$(".box").hover(function() {
		$(this).find('.cover').show();
	}, function() {
		$(this).find('.cover').hide();
	})
	/* 
		* parend 父级id
		* clsName 元素class
		* */
		function waterFall(parent, clsName){
			var firstLeft = 0, firstTop = 0;
			var $parent = $('#'+parent);//父元素
			var $boxs = $parent.find('.'+clsName);//所有box元素
			var iPinW = $boxs.eq( 0 ).width() + 10 + 14;// 一个块框box的宽
			var cols = Math.ceil( $parent.width() / iPinW );//列数

			var pinHArr=[];//用于存储 每列中的所有块框相加的高度。
			$boxs.each( function( index, dom){
				if( index < cols ){
					$boxs.eq(index).css({'position': 'absolute', 'top': '0px', 'left': firstLeft});
					firstLeft = firstLeft + iPinW;
					pinHArr[ index ] = $(dom).height(); //所有列的高度
					console.log('height:'+$(dom).height());
				}else{
					var minH = Math.min.apply( null, pinHArr );//数组pinHArr中的最小值minH
					var minHIndex = $.inArray( minH, pinHArr );
					$(dom).css({
						'position': 'absolute',
						'top': minH + 10+14,
				        'left': $boxs.eq( minHIndex ).position().left
				    });
				    //添加元素后修改pinHArr
				    pinHArr[ minHIndex ] += $(dom).height() + 10+14;//更新添加了块框后的列高
				}
			});
			var $lastBox = $boxs.last(),
				lastBoxH = $lastBox.offset().top + $lastBox.height()/4;
			$parent.height(lastBoxH);
		};
	var page = 0;
/*------------滚动加载第二页------*/
	$(window).scroll(function() {
		var windowH = $(window).height(),
		scrollTop = $(window).scrollTop(),
		documentH = $(document).height();
		if(scrollTop + windowH >= documentH-200) {
			ajax();
		}
	})
	function ajax() {
		page = page + 1;
		console.log(page);
		$.ajax({
			type:"get",
		    url:"/api/waterfall.php",
		    data: 'page=' + page,
		    success: function(reqData) {
		    	var dataJson = JSON.parse(reqData)
		       	console.log(reqData)
		       	console.log(dataJson.data);
				create(dataJson.data);
		    },
		    error: function(msg) {
				console.log("失败");
		        console.log(msg)
		    }
		});
	}
}) 